package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.List;

/**
 * @Author FengHuan Wang
 * @Date 2024/12/29 16:31
 * @Version 1.0
 */
@Service
public class DishServiceimpl implements DishService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Override
    public void save(DishDTO dishDTO) {

        Dish dish = new Dish();
        //将dishDTO的属性拷贝到Dish中
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.insert(dish);
        //获取insert插入方法返回的主键ID
        Long id = dish.getId();
        //将flavordish插入到数据库中
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors.size() != 0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(id);
            });
        }
        dishFlavorMapper.insertByFlavor(flavors);
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        /*菜品分页查询，使用分页插件*/
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        /*调用mapper层的接口进行分页查询*/
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /*根据id来批量删除菜品*/
    @Override
    @Transactional
    public void deleteById(List<Long> ids) {
        /*业务规则：
        * 1、可以一次删除一个菜品，也可以批量删除菜品
        * 2、起售中的菜品不能删除
        * 3、被套餐关联的菜品不能删除
        * 4、删除菜品后，关联的口味数据也需要删除掉*/

        List<Dish> dishList =  dishMapper.getById(ids);
        Long setMealList =  setMealDishMapper.getByDishIds(ids);
        //对查询到的菜品进行遍历，并根据起售状态选择性删除
        for (Dish dish : dishList) {
            if (dish.getStatus().equals(StatusConstant.ENABLE) ) {
                //代表起售状态，不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //被套餐关联的菜品不能删除
        if (setMealList != 0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        List<Long> idList = ids;
        //删除当前list集合中的菜品
        dishMapper.deleteByIds(idList);
        //删除相关联口味的数据
        dishFlavorMapper.deleteByDishIds(idList);
    }

    /*菜品查询回显*/
    @Override
    public DishVO getById(Long dishId) {
        /*查询回显：
        * 1、先查询Dish表中的数据
        * 2、再查询Flavor表中的口味数据
        * 3、最后封装成一个VO对象返回给前端*/
        Dish dish =  dishMapper.getByDishId(dishId);
        List<DishFlavor> dishFlavorList =  dishFlavorMapper.getByDishId(dishId);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavorList);
        return dishVO;
    }

    @Override
    public void updateDishById(DishVO dishVO) {
        /*修改菜品分为两步：
        * 1、先修改Dish菜品表中的数据
        * 2、再修改Flavor表中的数据*/
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishVO,dish);
        //修改菜品表中的数据
        dishMapper.updateDishById(dish);
        Long id = dish.getId();
        //修改口味表中的数据,先删除，再插入的方式
        //将flavordish插入到数据库中
        List<DishFlavor> flavors = dishVO.getFlavors();
        if (flavors.size() != 0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(id);
            });
            dishFlavorMapper.insertByFlavor(flavors);
        }
    }

    @Override
    public List<Dish> getByCategoryId(Long categoryId ) {
        List<Dish> dishList =  dishMapper.getByCategoryId(categoryId);
        return dishList;
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    /*菜品的起售停售*/
    @Override
    public void stopAndStop(Integer status, Long id) {
        dishMapper.stopAndStart(status,id);
    }
}
